import { defineConfig as defineConfigVite, ConfigEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import historyFallback from './vite-plugin-history';
import copyPlugin from 'rollup-plugin-copy';

const pathRewritesConfig = [
  {
    from: '/openFile',
    to: `/openFile.html`,
  },
  {
    from: '/home',
    to: `/home.html`,
  },
  {
    from: '/price',
    to: `/price.html`,
  },
  {
    from: '/player',
    to: `/player.html`,
  },
  // '/'重定向首页需要最后
  {
    from: '/',
    to: `/home.html`,
  },
];

// https://vitejs.dev/config/
const defineConfig = defineConfigVite as any; // 重载
const viteConfig = defineConfig((mode: any) => {
  const moduleName = (mode as ConfigEnv).mode;
  return {
    server: {
      host: '0.0.0.0',
      hmr: true,
      port: 7814,
      open: '/',
      cors: true,
    },
    root: resolve(__dirname, './src/microapps'), // 配置了这个之后, publicDir和build.outDir都相对此路径
    plugins: [
      vue(),
      historyFallback({
        rewrites: pathRewritesConfig,
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
      extensions: ['.ts', '.js', '.vue', '.json'],
    },
    base: './', // 设置打包路径
    publicDir: resolve(__dirname, './public_' + moduleName),
    build: {
      emptyOutDir: false, // 将此配置项设为false即可
      rollupOptions: {
        input: {
          [moduleName]: resolve(__dirname, `./src/microapps/${moduleName}.html`),
        },
        output: {
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: (assetInfo: any) => {
            if (/\.json/.test(assetInfo.name)) {
              return './[name].json';
            }
            return '[ext]/[name]-[hash].[ext]';
          },
          // '[ext]/[name]-[hash].[ext]',
        },
        plugins: [
          copyPlugin({
            verbose: true,
            hook: 'writeBundle',
            targets: process.env.OS?.includes('Windows')
              ? [
                  {
                    src: `src/microapps/${moduleName}/plugin.json`,
                    dest: `build/${moduleName}/`,
                  },
                  {
                    src: `src/microapps/${moduleName}/preload.js`,
                    dest: `build/${moduleName}/`,
                  },
                  {
                    src: `src/microapps/${moduleName}/logo.png`,
                    dest: `build/${moduleName}/`,
                  },
                ]
              : [
                  {
                    src: resolve(__dirname, `./src/microapps/${moduleName}/plugin.json`),
                    dest: resolve(__dirname, `./build/${moduleName}/`),
                  },
                  {
                    src: resolve(__dirname, `./src/microapps/${moduleName}/preload.js`),
                    dest: resolve(__dirname, `./build/${moduleName}/`),
                  },
                  {
                    src: resolve(__dirname, `./src/microapps/${moduleName}/logo.png`),
                    dest: resolve(__dirname, `./build/${moduleName}/`),
                  },
                ],
          }),
        ],
      },
      outDir: resolve(__dirname, `./build/${moduleName}`),
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
        },
      },
    },
  };
});

export default viteConfig;
